package controller;

import entity.Cart;
import entity.User;
import service.CartService;
import service.impl.CartServiceImpl;
import util.Constants;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * @version 1.0
 * @Author: xxf
 * @CreateDate: 2020/10/6 18:34
 */
@WebServlet("/cart")
public class CartController extends BaseController {
    private CartService service = new CartServiceImpl();

    /**
     * 添加购物车
     * @param request
     * @param response
     * @return
     */
    public String add(HttpServletRequest request, HttpServletResponse response) {

        //判断是否登录
        HttpSession session = request.getSession();
        User loginUser = (User) session.getAttribute("loginUser");

        if (loginUser == null) {

            session.setAttribute("msg", "请先登录");
            return Constants.REDIRECT + "/login.jsp";
        }
        //获取参数--》商品ID和用户ID
        String pid = request.getParameter("pid");
        int uid = loginUser.getUid();


        //调用service对象

        try {
            service.addCart(Integer.parseInt(pid), uid);
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        //响应

        return Constants.FORWARD + "/cartSuccess.jsp";
    }

    /**
     * 展示购物车
     * @param request
     * @param response
     * @return
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public String show(HttpServletRequest request, HttpServletResponse response) throws InvocationTargetException, IllegalAccessException {

        //判断是否登录
        HttpSession session = request.getSession();
        User loginUser = (User) session.getAttribute("loginUser");

        if (loginUser == null) {

            session.setAttribute("msg", "请先登录");
            return Constants.REDIRECT + "/login.jsp";
        }

        //获取参数
        int uid = loginUser.getUid();
        //调用service业务
        List<Cart> list =service.findAll(uid);

        //响应
        request.setAttribute("list", list);
        return Constants.FORWARD + "/cart.jsp";
    }

    /**
     * 删除购物车
     * @param request
     * @param response
     * @return
     */
    public String delete(HttpServletRequest request, HttpServletResponse response) {

        //获取参数
        String cid = request.getParameter("cid");
        //调用service
        service.delCartByCid(Integer.parseInt(cid));

        //响应

        return Constants.FORWARD + "/cart?method=show";
    }

    /**
     * 修改购物车
     * @param request
     * @param response
     * @return
     */
    public String update(HttpServletRequest request, HttpServletResponse response) {

        //获取参数
        String cid = request.getParameter("cid");
        String cnum = request.getParameter("cnum");
        String price = request.getParameter("price");
        //调用service
        service.updateCart(Integer.parseInt(cid),Integer.parseInt(cnum),Integer.parseInt(price));

        //响应

        return Constants.FORWARD + "/cart?method=show";
    }
    /**
     * 清空购物车
     * @param request
     * @param response
     * @return
     */
    public String clear(HttpServletRequest request, HttpServletResponse response) {

        //获取参数
        String uid = request.getParameter("uid");

        //调用service
        service.clearCart(Integer.parseInt(uid));

        //响应

        return Constants.FORWARD + "/cart?method=show";
    }


}
